//
//  RevampView.m
//  JackDeve
//
//  Created by apple on 17/5/10.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "RevampView.h"

@implementation RevampView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithRed:(241/255.0) green:(241/255.0) blue:(241/255.0) alpha:1];
         [self addSubview:self.backV];
         [self.backV addSubview:self.reminderL];
        [self.backV addSubview:self.accountNameTF];
         [self addSubview:self.remarkL];
        
        
        self.backV.sd_layout
        .topSpaceToView(self,84)
        .leftSpaceToView(self,15)
        .rightSpaceToView(self,15)
        .heightIs(50);
        
        self.reminderL.sd_layout
        .topEqualToView(self.backV)
        .leftEqualToView(self.backV)
        .widthIs(100)
        .bottomEqualToView(self.backV);

        self.accountNameTF.sd_layout
        .topEqualToView(self.backV)
        .leftSpaceToView(self.reminderL,0)
        .rightEqualToView(self.backV)
        .bottomEqualToView(self.backV);
        
        self.remarkL.sd_layout
        .topSpaceToView(self.backV,10)
        .leftSpaceToView(self,20)
        .rightSpaceToView(self,20)
        .autoHeightRatio(0);
    }
    return self;
}

-(UITextField *)accountNameTF{
    if (_accountNameTF==nil) {
        _accountNameTF     = [[UITextField alloc] init];
        _accountNameTF.delegate          = self;
        _accountNameTF.clearButtonMode   = UITextFieldViewModeAlways;
       
      
    }
    return _accountNameTF;
}
-(UIView *)backV{
    if (_backV==nil) {
        
        _backV          = [[UIView alloc] init];
        _backV.layer.borderWidth = 1;
        _backV.layer.borderColor = [UIColor colorWithRed:(233/255.0) green:(109/255.0) blue:(7/255.0) alpha:1].CGColor;
        _backV.backgroundColor   = [UIColor whiteColor];
        
    }
    return _backV;
}
-(UILabel *)remarkL{
    if (_remarkL==nil) {
        
        _remarkL = [[UILabel alloc] init];
        _remarkL.text     = @"以英文字母或汉字开头,限4-16个字符,一个汉字为一个字符";
        _remarkL.font     = [UIFont systemFontOfSize:13];
    }
    return _remarkL;
}
-(UILabel *)reminderL{
    if (_reminderL==nil) {
        _reminderL      = [[UILabel alloc] init];
        _reminderL.text  = @"用户名:";
        _reminderL.textAlignment = NSTextAlignmentCenter;
    }
    return _reminderL;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (textField == _accountNameTF) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        }
        //so easy
        else if (_accountNameTF.text.length >= 16) {
            _accountNameTF.text = [textField.text substringToIndex:16];
            return NO;
        }
        //首字母不能为数字
        NSString *regex = @"[0-9]+";
        
        NSPredicate*predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
        
        if ([predicate evaluateWithObject:_accountNameTF.text]) {
            _accountNameTF.text = [textField.text substringToIndex:0];
            if (self.presentBlock != nil) {
                self.presentBlock(YES);
            }
            
            return NO;
           
        }
        
        
        
    }
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self endEditing:YES];
}
@end
